package cn.ulc.socket.admin.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.PubDataConstant;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.common.util.Result;
import cn.ulc.socket.admin.model.dto.staff.StaffListDTO;
import cn.ulc.socket.admin.model.param.staff.StaffSaveParam;
import cn.ulc.socket.admin.service.staff.StaffServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;

@RestController
@RequestMapping(value = "/users")
public class StaffCtrl {
    private static final Log log = LogFactory.get();

    @Autowired
    StaffServ userServ;


    @GetMapping("/rider/list")
    public Result findRiderList(String key, PaginationParam pager){
        StaffListDTO dto = userServ.findRiderList(key,pager);
        return Result.success(dto);
    }

    @GetMapping("/rider/all")
    public Result findUserList(String key){
        StaffListDTO dto = userServ.findListByIdentity(key, PubDataConstant.TYPE_ID_RIDER);
        return Result.success(dto);
    }

    @GetMapping("/rider/detail/{id}")
    public Result getRider(@PathVariable @Min(1) Long id){
        return Result.success(userServ.findOne(id));
    }


    @PostMapping("/rider")
    public Result saveRider(@Validated @RequestBody StaffSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存用户详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(userServ.saveRider(param));
    }

    @PutMapping("/rider/{id}")
    public Result updateRider(@PathVariable("id") Long id, @RequestBody StaffSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存用户详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        param.setId(id);
        return Result.success(userServ.saveRider(param));
    }
    @GetMapping
    public Result findList(String key, PaginationParam pager) {
        StaffListDTO dto = userServ.findList(key,pager);
        return Result.success(dto);
    }

    @GetMapping("/{id}")
    public Result findDetail(@PathVariable Long id) {
        return Result.success(userServ.findDetail(id));
    }

    @PostMapping
    public Result save(@Validated @RequestBody StaffSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存用户详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(userServ.save(param));
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Long id, @RequestBody StaffSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存用户详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        param.setId(id);
        return Result.success(userServ.save(param));
    }

    @PutMapping("/resetPassword/{id}")
    public Result resetPassword(@PathVariable("id") Long id){
        return Result.success(userServ.resetPassword(id));
    }

    @DeleteMapping("/{id}")
    public Result deleted(@PathVariable("id") Long id){
        return Result.success(userServ.deleted(id));
    }

    @PostMapping("/generateQrCode/{id}")
    public Result generateQrCode(@PathVariable("id") Long id){
        return Result.success(userServ.generateQrCode(id));
    }
    @PostMapping("/relieveQrCode/{id}")
    public Result relieveQrCode(@PathVariable("id") Long id){
        return Result.success(userServ.relieveQrCode(id),true);
    }
    @GetMapping("/checkBind/{id}")
    public Result checkBind(@PathVariable("id") Long id){
        return Result.success(userServ.checkBind(id),true);
    }
}
